package com.mathdroid.gui.main.abilities.optimizer;

import java.awt.Dimension;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.beans.PropertyChangeListener;

import javax.swing.JButton;
import javax.swing.JPanel;
import javax.swing.SwingWorker;

import com.mathdroid.exceptionhandling.DroidClipboard;
import com.mathdroid.gui.componentlist.ComponentList;
import com.mathdroid.gui.main.AOptimizerFrame;
import com.mathdroid.model.abilities.optimizer.AbilityOptimizerResult;
import com.mathdroid.model.abilities.optimizer.AbilityOptimizerResultList;

/**
 * This is shown when the ability optimizer does his work in the background.
 * After the work is done, the results are shown.
 */
public final class AbilityOptimizerFrame 
extends AOptimizerFrame<AbilityOptimizerResultList, Void>
implements PropertyChangeListener {

	/**
	 * Default UID.
	 */
	private static final long serialVersionUID = 1L;
	
	/**
	 * The result list.
	 */
	private ComponentList list = new ComponentList();
	
	/**
	 * The content panel.
	 */
	private JPanel p = null;
	
	/**
	 * The constraints for UI components.
	 */
	private GridBagConstraints c = new GridBagConstraints();
	
	/**
	 * A button to copy selected text to the clip board.
	 */
	private JButton copy = new JButton("Copy results to clipboard");
	
	/**
	 * The weight of larger UI components.
	 */
	private static final int HIGH_WEIGHT = 10;
	
	/**
	 * The max value of progress bar.
	 */
	private static final int MAX_PROGRESS = 100;
	
	/**
	 * The amount of columns of the layout grid.
	 */
	private static final int COLS = 1;
	
	/**
	 * The results of the task.
	 */
	private AbilityOptimizerResultList results = null;
	
	/**
	 * Create a new ability optimizer view.
	 * @param t The task to get progress information from.
	 */
	public AbilityOptimizerFrame(
			final SwingWorker<AbilityOptimizerResultList, Void> t) {
		super(t, 0, MAX_PROGRESS, "Ability Priority Optimization");
		
		this.p = this.getContent();
		
		this.p.setLayout(new GridBagLayout());
		
		this.buildList();
		this.buildCopy();
		
		final int width = 600;
		final int height = 400;
		
		this.p.setPreferredSize(new Dimension(width, height));
	}

	/**
	 * Build the result list.
	 */
	private void buildList() {
		this.c.fill = GridBagConstraints.BOTH;
		this.c.gridx = 0;
		this.c.gridy = 0;
		this.c.weightx = HIGH_WEIGHT;
		this.c.weighty = HIGH_WEIGHT;
		this.c.gridwidth = COLS;
		this.c.gridheight = 1;
		
		this.list.setComponentListModel(new ResultListModel());
		this.list.setComponentListCellRenderer(new ResultListRenderer());
		
		this.p.add(this.list, this.c);
	}

	/**
	 * Build the "copy" button.
	 */
	private void buildCopy() {
		this.c.fill = GridBagConstraints.HORIZONTAL;
		this.c.gridx = 0;
		this.c.gridy = 1;
		this.c.weightx = 1;
		this.c.weighty = 0;
		this.c.gridwidth = 1;
		this.c.gridheight = 1;
		
		this.copy.setIcon(this.getImageLoader().getImage("clipboard_sign"));
		
		this.copy.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(final ActionEvent e) {
				new DroidClipboard().copy(results.toString());
			}
		});
		
		this.p.add(this.copy, this.c);
	}

	@Override
	protected void done(final AbilityOptimizerResultList r) {
		final double maxRating = r.get(0).getRating();
		this.results = r;
		
		for (AbilityOptimizerResult result : r) {
			this.list.addElement(new ResultElement(result, maxRating));
		}
	}
}
